<?php 
//Billing
$bill_total = 0;        $count_total = 0;       $cust_total = 0;        $m3_total = 0;
$pbillTotal = 0;        $pcountTotal = 0;       $pcustTotal = 0;        $pm3Total = 0;
$bill_month = "No Billing Record";
foreach ($bill_data as $key) {
    $bill_month = "Month of ".date('M-Y',strtotime($key->yearMonth));
    $bill_total = $bill_total + $key->bill_amt;
    $count_total = $count_total + $key->bill_count;
    $cust_total = $cust_total + $key->custs_count;
    $m3_total = $m3_total + $key->qty;
}
foreach ($prv_bills as $key) {
    $pbillTotal = $pbillTotal + $key->bill_amt;
    $pcountTotal = $pcountTotal + $key->bill_count;
    $pcustTotal = $pcustTotal + $key->custs_count;
    $pm3Total = $pm3Total + $key->qty;
}

if($pbillTotal == 0) $prvDif        = "N/A"; else $prvDif       = round(($bill_total - $pbillTotal)*100/$pbillTotal,2)."%";
if($pm3Total   == 0) $m3Dif         = "N/A"; else $m3Dif        = round(($m3_total-$pm3Total)*100/$pm3Total,2)."%";
if($cust_total == 0) $percentBill   = "N/A"; else $percentBill  = round(($count_total * 100/$cust_total),2)."%";




// var_dump($pbillTotal);

//Collection
$coll_total = 0;    $toCollect  = 0; 
$pcollTotal = 0;    $ptoCollect = 0;
$coll_month = "No Collection Record";
foreach ($coll_data as $key) {
    $coll_month = "Month of ".date('M-Y',strtotime($key->yearMonth));
    $coll_total = $coll_total + $key->pay_amt;
    $toCollect  = $toCollect  + $key->bill_amt;
}
foreach ($prv_colls as $key) {
    $pcollTotal = $pcollTotal + $key->pay_amt;
    $ptoCollect  = $ptoCollect  + $key->bill_amt;
}

if($toCollect == 0) $percentColl    = "N/A"; else $percentColl  = round($coll_total * 100 / $toCollect,2)."%";
if($pcollTotal == 0) $colDif        = "N/A"; else $colDif       = round(($coll_total - $pcollTotal) * 100/$pcollTotal,2)."%";



// Half Year Revenue
$months = array();      $ctr = 0;
$collected = array();   $str = 0;   $cnt = 0;
$unCollect = array();   $utr = 0;
foreach ($hyear_rev as $key) {
    
    if($ctr == 0){
        $months[$ctr] = date('M',strtotime($key->yearMonth));
        $str = $str + $key->pay_amt;            $utr = $utr + $key->bill_amt;
        $ctr++;
        //echo "2-";
    }else if($months[$ctr-1] != date('M',strtotime($key->yearMonth))){
        $months[$ctr] = date('M',strtotime($key->yearMonth));
        $collected[$cnt] = $str; $str = 0;      $unCollect[$cnt] = $utr; $utr = 0;
        $str = $str + $key->pay_amt;            $utr = $utr + $key->bill_amt;
        $cnt++;                                 $ctr++;
        //echo "3-";
    }else{
        $str = $str + $key->pay_amt;
        $utr = $utr + $key->bill_amt;
        //echo "4-";
    }

    if($key === end($hyear_rev)){
        //$str = $str + $key->pay_amt;            $utr = $utr + $key->bill_amt;
        $collected[$cnt] = $str;                $unCollect[$cnt] = $utr;
        //echo "1-";
    }
}
$total_collected = 0;           $total_billed = 0;
foreach ($collected as $key) {
   $total_collected = $total_collected + $key;
}
foreach ($unCollect as $key) {
   $total_billed = $total_billed + $key;
}
$total_unCollect = $total_billed - $total_collected;
if($total_billed == 0) $totalPrcntCol = "NA"; else $totalPrcntCol = round(($total_collected * 100)/$total_billed,2)."%";

$mCollected = array();   $ctr1   = 0; $cnt1 =0;
$cMonths = array();      $str1   = 0; $cTotal=0;
foreach ($cyear_rev as $key) {
    
    if($ctr1 == 0){
        $cMonths[$ctr1] = date('M',strtotime($key->ym));
        $str1 = $str1 + $key->amt; 
        // echo "elseif".' - '.date('M',strtotime($key->ym)) .' and ' . $cMonths[$ctr1];
        //$mCollected[$ctr1] = $key->amt;          
        $ctr1++; 
        //echo "fist";

    }else if($cMonths[$ctr1-1] != date('M',strtotime($key->ym))){
        // echo "elseif".' - '.date('M',strtotime($key->ym)) .' and ' . $cMonths[$ctr1-1] . ' amount ' . $key->amt ;
        //echo "second";
        $cMonths[$ctr1] = date('M',strtotime($key->ym));
        
        // echo "current". ' - '.count($mCollected);
        //echo "val".$key->amt;
        $mCollected[$cnt1] = $str1;$cTotal = $cTotal + $str1; $str1 = 0;
        $str1 = $str1 + $key->amt;
        $cnt1++;                                 $ctr1++;
    }else{
        //echo "third";
        $str1 = $str1 + $key->amt;
       // $utr = $utr + $key->bill_amt;
        //echo $key->pay_amt.'-'.$key->yearMonth;
    }
    if($key === end($cyear_rev)){
        $str1 =0;
        $str1 = $str1 + $key->amt;            //$utr = $utr + $key->bill_amt;
        $mCollected[$cnt1] = $str1; 
        $cTotal = $cTotal + $str1;
       // $cMonths[$ctr1] = date('M',strtotime($key->ym));    
        //echo "last";  
        //break;       //  $unCollect[$cnt1] = $utr;
    }
}

//var_dump($collected);
$sample = array('q','w','a','q','w','d');
?>


<div class="row animated fadeInRight">
    <div class="wrapper wrapper-content">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="ibox float-e-margins">
                            <div class="ibox-content">
                                    <div>
                                        <span class="pull-right text-right">
                                        <small>Collection Percentage : <strong><?php echo $totalPrcntCol; ?></strong></small>
                                            <br>
                                            <small>Total Uncollected Amt</small> : <strong>P <?php echo number_format($total_unCollect,2,".",','); ?></strong>
                                        </span>
                                        
                                        <h3 class="m-b-xs" style="color:rgba(248,172,89,1);">P<?php echo number_format($cTotal,2,".",","); ?></h3>
                                        <h4 class="font-bold no-margins">
                                            Half-year Revenue
                                        </h4>
                                        <h3 class="m-b-xs" style="color:rgba(26,179,148,1);">P<?php echo number_format($total_collected,2,".",","); ?></h3>
                                        <h4 class="font-bold no-margins">
                                            Half-year Collection Performance
                                        </h4>
                                        <small></br></small>
                                    </div>

                                <div>
                                    <canvas id="lineChart" height="191" width="819" style="width: 819px; height: 191px;"></canvas>
                                </div>

                                <div class="m-t-md">
                                    <small class="pull-right">
                                       <!--  <i class="fa fa-clock-o"> </i> -->
                                        
                                    </small>
                                   <small>
                                       <strong>Analysis of collection</strong>
                                   </small>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>


                <div class="row">
                    <div class="col-lg-4">
                        <div class="ibox float-e-margins"id="front_coll" style="cursor: pointer;">
                            <div class="ibox-title">
                                <span class="label label-warning pull-right"><?php echo $coll_month; ?></span>
                                <h5>Collection</h5>
                            </div>
                            <div class="ibox-content">
                                <h1 class="no-margins">P <?php echo  number_format($coll_total,2,".",","); ?></h1>
                                <div class="stat-percent font-bold text-info"><?php echo $percentColl; ?>
                                    <!-- <i class="fa fa-level-up"></i> -->
                                </div>
                                <small>Percent Collected</small></br>
                                    <?php if($colDif < 0) {?>
                                    <div class="stat-percent font-bold text-danger"><?php echo $colDif; ?> 
                                    <i class="fa fa-level-down"></i>
                                    <?php }else if($colDif == 'N/A') {?>
                                    <div class="stat-percent font-bold text-danger"><?php echo $colDif; ?> 
                                    <i class="fa fa-bolt"></i>
                                    <?php }else{?>
                                    <div class="stat-percent font-bold text-navy"><?php echo $colDif; ?> 
                                    <i class="fa fa-level-up"></i>
                                    <?php } ?>
                                </div>
                                <small>Change from Previous Month</small>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <div class="ibox float-e-margins" id="front_bill" style="cursor: pointer;">
                            <div class="ibox-title">
                                <span class="label label-primary pull-right"><?php echo $bill_month; ?></span>
                                <h5>Billed Amount</h5>
                            </div>
                            <div class="ibox-content">
                                <h1 class="no-margins">P <?php echo  number_format($bill_total,2,".",","); ?></h1>
                                <div class="stat-percent font-bold text-navy"><?php echo $percentBill; ?> 
                                    <!-- <i class="fa fa-level-up"></i> -->
                                </div>
                                <small>Billing Compeletion</small></br>
                                    <?php if($prvDif < 0) {?>
                                    <div class="stat-percent font-bold text-danger"><?php echo $prvDif; ?>
                                    <i class="fa fa-level-down"></i>
                                    <?php }else if($prvDif == 'N/A') {?>
                                    <div class="stat-percent font-bold text-danger"><?php echo $prvDif; ?> 
                                    <i class="fa fa-bolt"></i>
                                    <?php }else{?>
                                    <div class="stat-percent font-bold text-navy"><?php echo $prvDif; ?>
                                    <i class="fa fa-level-up"></i>
                                    <?php } ?>
                                </div>
                                <small>Change from Previous Month</small>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <div class="ibox float-e-margins" id="front_bill2" style="cursor: pointer;">
                            <div class="ibox-title">
                                <span class="label label-info pull-right"><?php echo $bill_month; ?></span>
                                <!-- <h5>Billed m<sup>3</sup></h5> -->
                                <h5>Volume Billed</h5>
                            </div>
                            <div class="ibox-content">
                                <h1 class="no-margins"><?php echo  number_format($m3_total,0,".",","); ?> m<sup>3</sup></h1>
                                <!-- <div class="stat-percent font-bold text-navy"><?php echo $percentBill; ?> 
                                    <i class="fa fa-level-up"></i>
                                </div>
                                <small>Billing Compeletion</small> -->
                            </br>
                                    <?php if($m3Dif < 0) {?>
                                    <div class="stat-percent font-bold text-danger"><?php echo $m3Dif; ?> 
                                    <i class="fa fa-level-down"></i>
                                    <?php }else if($m3Dif == 'N/A') {?>
                                    <div class="stat-percent font-bold text-danger"><?php echo $m3Dif; ?> 
                                    <i class="fa fa-bolt"></i>
                                    <?php }else{?>
                                    <div class="stat-percent font-bold text-navy"><?php echo $m3Dif; ?> 
                                    <i class="fa fa-level-up"></i>
                                    <?php } ?>
                                </div>
                                <small>Change from Previous Month</small>
                            </div>
                        </div>
                    </div>
                    
                </div>
                <div class="row" style="display:none;">
                    <div class="col-lg-12">
                        <div class="ibox float-e-margins">
                            <div class="ibox-content">
                                    <div>
                                        <!-- <span class="pull-right text-right">
                                        <small>Collection Percentage : <strong><?php echo $cTotal; ?></strong></small>
                                            <br>
                                            Total Uncollected Amount : <strong>P <?php echo number_format($total_unCollect,2,".",','); ?></strong>
                                        </span> -->
                                        <h2 class="m-b-xs">P <?php echo number_format($cTotal,2,".",","); ?></h2>
                                        <h4 class="font-bold no-margins">
                                            Half-year Cash Flow
                                        </h4>
                                        <small></br></small>
                                    </div>

                                <div>
                                    <canvas id="lineChart2" height="191" width="819" style="width: 819px; height: 150px;"></canvas>
                                </div>

                                <div class="m-t-md">
                                    <small class="pull-right">
                                       <!--  <i class="fa fa-clock-o"> </i> -->
                                        
                                    </small>
                                   <small>
                                       <strong>Analysis of collection</strong>
                                   </small>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>

                

            </div>
</div>
<script>
    $(document).ready(function() {
            var la = new Array();
            var bl = new Array();
            var cl = new Array();
            var sm = new Array();
            var ctr = 0;
            var col_m = new Array();
            var col_c = new Array();

            col_m = <?php echo json_encode($cMonths); ?>;
            col_c = <?php echo json_encode($mCollected); ?>;
            la = <?php echo json_encode($months); ?>;
            bl = <?php echo json_encode($unCollect); ?>;
            cl = <?php echo json_encode($collected); ?>;
            sm = <?php echo json_encode($sample); ?>;
             Chart.numberWithCommas = function(x) {
                return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
              };


            var lineData = {
                labels: la,
                prc: sm,
                datasets: [
                    {
                        label: "Revenue",
                        fillColor: "rgba(248,172,89,0.25)",
                        strokeColor: "rgba(248,172,89,1)",
                        pointColor: "rgba(248,172,89,1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(248,172,89,1)",
                        data: col_c
                    },
                    {
                        label: "Billed Amount",
                        fillColor: "rgba(35,131,200,0.2)",
                        strokeColor: "#1a82b3",
                        pointColor: "#1a82b3",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "#1a82b3",
                        data: bl
                    },
                    {
                        label: "Collected Amount",
                        fillColor: "rgba(26,179,148,0.5)",
                        strokeColor: "rgba(26,179,148,0.7)",
                        pointColor: "rgba(26,179,148,1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(26,179,148,1)",
                        data: cl
                    }
                ]
            };

            var lineOptions = {
                scaleShowGridLines: true,
                scaleGridLineColor: "rgba(0,0,0,.05)",
                scaleGridLineWidth: 1,
                bezierCurve: true,
                bezierCurveTension: 0.4,
                pointDot: true,
                pointDotRadius: 4,
                pointDotStrokeWidth: 1,
                pointHitDetectionRadius: 20,
                datasetStroke: true,
                datasetStrokeWidth: 2,
                datasetFill: true,
                responsive: true,
                multiTooltipTemplate: "<%= datasetLabel %> - <%= value.toLocaleString() %>",
                scaleLabel: "<%=Chart.numberWithCommas(value)%>"
            };

            Chart.prcs = function(d){
                return d.label + "-" +lineData.prc[lineData.labels.indexOf(d.label)]
            };

            var ctx = document.getElementById("lineChart").getContext("2d");
            var myNewChart = new Chart(ctx).Line(lineData, lineOptions);


            // var lineData2 = {
            //     labels: col_m,
            //     prc: sm,
            //     datasets: [
            //         {
            //             label: "Amount",
            //             fillColor: "rgba(26,179,148,0.5)",
            //             strokeColor: "rgba(26,179,148,0.7)",
            //             pointColor: "rgba(26,179,148,1)",
            //             pointStrokeColor: "#fff",
            //             pointHighlightFill: "#fff",
            //             pointHighlightStroke: "rgba(26,179,148,1)",
            //             data: col_c
            //         },
            //         {
            //             label: " ",
            //             fillColor: "rgba(26,179,148,0.5)",
            //             strokeColor: "rgba(26,179,148,0.7)",
            //             pointColor: "rgba(26,179,148,1)",
            //             pointStrokeColor: "#f7bbda",
            //             pointHighlightFill: "#f7bbda",
            //             pointHighlightStroke: "rgba(26,179,148,1)",
            //             data: ''
            //         }
            //     ]
            // };

            // var lineOptions2 = {
            //     scaleShowGridLines: true,
            //     scaleGridLineColor: "rgba(0,0,0,.05)",
            //     scaleGridLineWidth: 1,
            //     bezierCurve: true,
            //     bezierCurveTension: 0.4,
            //     pointDot: true,
            //     pointDotRadius: 4,
            //     pointDotStrokeWidth: 1,
            //     pointHitDetectionRadius: 20,
            //     datasetStroke: true,
            //     datasetStrokeWidth: 2,
            //     datasetFill: true,
            //     responsive: true,
            //     multiTooltipTemplate: "<%= datasetLabel %> - <%= value.toLocaleString() %>",
            //     scaleLabel: "<%=Chart.numberWithCommas(value)%>"
            // };

            // Chart.prcs = function(d){
            //     return d.label + "-" +lineData2.prc[lineData2.labels.indexOf(d.label)]
            // };

            // var ctx2= document.getElementById("lineChart2").getContext("2d");
            // var myNewChart2 = new Chart(ctx2).Line(lineData2, lineOptions2);

        $('#front_bill').click(function(){
            location.assign("<?php echo base_url('main/reroute/307/-1/35');?>");
        });
        $('#front_bill2').click(function(){
            location.assign("<?php echo base_url('main/reroute/307/-1/35');?>");
        });
        $('#front_coll').click(function(){
            location.assign("<?php echo base_url('main/reroute/307/-1/36');?>");
        });


        //"<%= datasetLabel %> -("+sm+") <%= value.toLocaleString() %>",

        });

    
</script>